package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/storageRecord")
public class StorageRecordController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(StorageRecordController.class);
    @Autowired
    StorageRecordService storageRecordService;
    @Autowired
    SupplierService supplierService;

    @RequestMapping("/page")
    public String page(Model model, StorageRecord condition, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        try {
            Page<StorageRecord> pageInfo = storageRecordService.getPage(condition, pageNo, pageSize);
            List<Supplier> supplierList = supplierService.findAll();
            model.addAttribute("condition", condition);
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("supplierList", supplierList);
            return "storageRecord/page";
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.page(condition, pageNo, pageSize)方法出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/save")
    public String toSave(Model model) {
        try {
            List<Supplier> supplierList = supplierService.findAll();
            model.addAttribute("supplierList", supplierList);
            //回显数据时需要,给个空对象避免出错
            model.addAttribute("storageRecord", new StorageRecord());
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.toSave(model)方法出现异常", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/save";
    }

    @PostMapping("/save")
    public String dosave(StorageRecord storageRecord) {
        SysUser loginUser = session(Constants.SESSION_USER);
        try {
            if (storageRecord.getId() == null) {
                //新增
                storageRecord.setCreatedTime(new Date());
                storageRecord.setCreatedUserId(loginUser.getId());
            } else {
                //修改
                storageRecord.setUpdatedTime(new Date());
                storageRecord.setUpdatedUserId(loginUser.getId());
            }
            boolean save = storageRecordService.save(storageRecord);
            if (save) {
                return redirect("/main/storageRecord/page");
            }
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.dosave(storageRecord)方法出现异常", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/save";
    }

    @GetMapping("/edit")
    public String toEdit(Model model, Integer id) {
        try {
            List<Supplier> supplierList = supplierService.findAll();
            model.addAttribute("supplierList", supplierList);
            StorageRecord storageRecord = storageRecordService.getById(id);
            model.addAttribute("storageRecord", storageRecord);
            model.addAttribute("supplierList", supplierList);
            return "storageRecord/save";
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.toEdit(model, id)方法出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    //验证是否唯一
    @GetMapping("/srCodeExist")
    public Map<String, Object> srCodeExist(String srCode) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(srCode)) {
            map.put("error", "不能为空");
            return map;
        } else
            try {
                //调用service层方法验证角色code是否唯一
                StorageRecord st = storageRecordService.getBySrCode(srCode);
                if (st == null) {
                    map.put("exist", 0);
                } else {
                    map.put("exist", 1);
                }
            } catch (Exception e) {
                LOGGER.error("执行StorageRecordController.srCodeExist(srCode)方法出现异常", e);
                throw new RuntimeException(e);
            }
        return map;
    }

    @GetMapping("/view/{id}")
    public String view(Model model, @PathVariable("id") Integer id) {
        try {
            StorageRecord storageRecord = storageRecordService.getById(id);
            model.addAttribute("storageRecord", storageRecord);
            return "storageRecord/view";
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.view(model, id)方法出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @DeleteMapping("/del/{id}")
    public Map<String, Object> del(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            boolean ok = storageRecordService.deleteById(id);
            if (ok) {
                map.put("code", 200);
                return map;
            }
        } catch (Exception e) {
            LOGGER.error("执行StorageRecordController.del(id)方法出现异常", e);
            throw new RuntimeException(e);
        }
        return map;
    }


}
